@echo off
rem -----------------------------------------------------------------
set current_dir=%~dp0%

set curl_dirname=curl-7.71.0
set curl_src_dir=%current_dir%%curl_dirname%
echo curl src dir: %curl_src_dir%

rem -----------------------------------------------------------------
rem 拷贝依赖
rem 需要编译openssl，zlib，nghttp
set build_output_prefix=%current_dir%build-output
set openssl_dep_path=%build_output_prefix%-openssl
set zlib_dep_path=%build_output_prefix%-zlib
set nghttp2_dep_path=%build_output_prefix%-nghttp2-1.41.0

set curl_deps=%current_dir%curl_deps
if exist %curl_deps% ( rmdir /S /Q %curl_deps% )
mkdir %curl_deps%
mkdir %curl_deps%\include
mkdir %curl_deps%\lib

call:copy_dep %openssl_dep_path%
call:copy_dep %zlib_dep_path%
call:copy_dep %nghttp2_dep_path%

call "%VS100COMNTOOLS%vsvars32.bat"

rem -----------------------------------------------------------------
rem 输出目录
set build_output_root=%current_dir%build-output-%curl_dirname%
set debug_output=%build_output_root%-debug
set release_output=%build_output_root%-release

if exist %build_output_root% ( rmdir /S /Q %build_output_root% )
mkdir %build_output_root%

rem -----------------------------------------------------------------
rem 解压代码
if exist %curl_src_dir% ( rmdir /S /Q %curl_src_dir% )
tar -xf %curl_src_dir%.zip

rem 编译release版
call:make_curl no %release_output%
mkdir %build_output_root%\include
xcopy /S /Y %release_output%\include %build_output_root%\include
mkdir %build_output_root%\lib\Release
copy /Y %release_output%\lib\libcurl_a.lib %build_output_root%\lib\Release\libcurl.lib

rem 编译debug版
call:make_curl yes %debug_output%
mkdir %build_output_root%\lib\Debug
copy /Y %debug_output%\lib\libcurl_a_debug.lib %build_output_root%\lib\Debug\libcurl.lib

::cd %curl_src_dir%\winbuild
::nmake /f Makefile.vc mode=static RTLIBCFG=static VC=10 WITH_SSL=static WITH_NGHTTP2=static WITH_ZLIB=static WITH_DEVEL=%curl_deps% WITH_PREFIX=%build_output_root% ENABLE_IPV6=yes ENABLE_WINSSL=no DEBUG=yes ENABLE_IDN=no MACHINE=x86
pause
exit /b 0

:copy_dep
set dep_src_dir=%~1
xcopy /S /Y %dep_src_dir%\include\* %curl_deps%\include
xcopy /S /Y %dep_src_dir%\lib\Release\*.lib %curl_deps%\lib
goto:eof

:: make_curl [yes|no] [output_dir]
:make_curl
set debug_config=%~1
set output_temp=%~2
if exist %output_temp% ( rmdir /S /Q %output_temp% )

cd %curl_src_dir%\winbuild
nmake /f Makefile.vc mode=static RTLIBCFG=static VC=10 WITH_SSL=static WITH_NGHTTP2=static WITH_ZLIB=static WITH_DEVEL=%curl_deps% WITH_PREFIX=%output_temp% ENABLE_IPV6=yes ENABLE_WINSSL=no DEBUG=%debug_config% ENABLE_IDN=no MACHINE=x86
cd %current_dir%

goto:eof